package com.rp.system.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rp.common.utils.RpUtil;
import com.rp.infopub.dao.ArticleDao;
import com.rp.infopub.dao.ChannelDao;
import com.rp.infopub.po.Article;
import com.rp.infopub.po.Channel;
import com.rp.system.dao.AuthorityDao;
import com.rp.system.dao.MenuDao;
import com.rp.system.dao.MenuItemDao;
import com.rp.system.dao.ModuleDao;
import com.rp.system.dao.ResourceDao;
import com.rp.system.dao.RoleDao;
import com.rp.system.po.Authority;
import com.rp.system.po.DatasourceConfig;
import com.rp.system.po.Menu;
import com.rp.system.po.MenuItem;
import com.rp.system.po.Module;
import com.rp.system.po.Resource;
import com.rp.system.po.Role;

@Service
@Transactional
public class DataImportService {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private MenuDao menuDao;
	@Autowired
	private MenuItemDao menuItenDao;
	@Autowired
	private AuthorityDao authorityDao;
	@Autowired
	private ModuleDao moduleDao;
	@Autowired
	private ResourceDao resourceDao;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private ChannelDao channelDao;
	@Autowired
	private ArticleDao articleDao;

	/**
	 * 导入菜单数据
	 * @param dbType
	 * @param url
	 * @param username
	 * @param password
	 */
	public void importMenu(DatasourceConfig dc){
		
		JdbcTemplate targetDbJdbcTemplate = this.createJdbcTemplate(dc);
		
		/******************************菜单导入******************************/
		
		//目标数据库中查询出的菜单列表
		List<Menu> targetMenus = targetDbJdbcTemplate.query("SELECT * FROM RP_SYSTEM_MENUS", 
				ParameterizedBeanPropertyRowMapper.newInstance(Menu.class));
		
		//本地数据库中查询出的菜单列表
		List<Menu> localMenus = menuDao.getAll();
		
		//临时保存待删除的本地已经拥有的菜单列表
		List<Menu> deleteMenus = new ArrayList<Menu>();
		for(int i=0;i<targetMenus.size();i++){
			if(localMenus.contains(targetMenus.get(i))){
				deleteMenus.add(targetMenus.get(i));
			}
		}
		targetMenus.removeAll(deleteMenus);
		
		for (Menu targetMenu : targetMenus) {
			Object[] params = new Object[]{
					targetMenu.getId(),
					targetMenu.getCreateDate(),
					targetMenu.getUpdateDate(),
					targetMenu.getVersion(),
					targetMenu.getName(),
					targetMenu.getIconCls(),
					targetMenu.getTitle(),
					targetMenu.getUrl(),
					targetMenu.getEnabled2()};
			jdbcTemplate.update(MenuDao.INSERT_MENU, params);
		}
		
		/******************************菜单项导入******************************/
		
		List<MenuItem> targetMenuItems = targetDbJdbcTemplate.query("SELECT * FROM RP_SYSTEM_MENUITEMS", 
				ParameterizedBeanPropertyRowMapper.newInstance(MenuItem.class));
		
		List<MenuItem> localMenuItems = menuItenDao.getAll();
		
		List<MenuItem> deleteMenuItems = new ArrayList<MenuItem>();
		for(int i=0;i<targetMenuItems.size();i++){
			if(localMenuItems.contains(targetMenuItems.get(i))){
				deleteMenuItems.add(targetMenuItems.get(i));
			}
		}
		targetMenuItems.removeAll(deleteMenuItems);
		
		for (MenuItem targetMenuItem : targetMenuItems) {
			Object[] params = new Object[]{
					targetMenuItem.getId(),
					targetMenuItem.getCreateDate(),
					targetMenuItem.getUpdateDate(),
					targetMenuItem.getVersion(),
					targetMenuItem.getMenuId(),
					targetMenuItem.getText(),
					targetMenuItem.getUrl(),
					targetMenuItem.getPid(),
					targetMenuItem.getIconCls(),
					targetMenuItem.getSort(),
					targetMenuItem.getVerifyClose()};
			jdbcTemplate.update(MenuItemDao.INSERT_MENUITEM, params);
		}
		
		targetDbJdbcTemplate = null;
		
		targetMenus.clear();
		localMenus.clear();
		deleteMenus.clear();
		
		targetMenuItems.clear();
		localMenuItems.clear();
		deleteMenuItems.clear();
		
	}
	
	/**
	 * 导入系统模块数据
	 * @param dc
	 */
	public void importModule(DatasourceConfig dc){
		
		JdbcTemplate targetDbJdbcTemplate = this.createJdbcTemplate(dc);
		
		//目标数据库中查询出的模块列表
		List<Module> targetModules = targetDbJdbcTemplate.query("SELECT * FROM RP_SYSTEM_MODULES", 
				ParameterizedBeanPropertyRowMapper.newInstance(Module.class));
		
		List<Module> localModules = moduleDao.getAll();
		
		//临时保存待删除的本地已经拥有的模块列表
		List<Module> deleteModules = new ArrayList<Module>();
		for(int i=0;i<targetModules.size();i++){
			if(localModules.contains(targetModules.get(i))){
				deleteModules.add(targetModules.get(i));
			}
		}
		targetModules.removeAll(deleteModules);
		
		for (Module targetModule : targetModules) {
			Object[] params = new Object[]{
					targetModule.getId(),
					targetModule.getCreateDate(),
					targetModule.getUpdateDate(),
					targetModule.getVersion(),
					targetModule.getName(),
					targetModule.getParentId(),
					targetModule.getRemark(),
					targetModule.getRescNum()};
			jdbcTemplate.update(ModuleDao.INSERT_MODULE, params);
		}
		
		targetDbJdbcTemplate = null;
		localModules.clear();
		deleteModules.clear();
		
	}
	
	/**
	 * 导入权限数据
	 * @param dc
	 */
	public void importAuthority(DatasourceConfig dc){
		
		JdbcTemplate targetDbJdbcTemplate = this.createJdbcTemplate(dc);
		
		//目标数据库中查询出的权限列表
		List<Authority> targetAuthorities = targetDbJdbcTemplate.query("SELECT * FROM RP_SYSTEM_AUTHORITIES", 
				ParameterizedBeanPropertyRowMapper.newInstance(Authority.class));
		
		List<Authority> localAuthorities = authorityDao.getAll();
		
		//临时保存待删除的本地已经拥有的权限列表
		List<Authority> deleteAuthorities = new ArrayList<Authority>();
		for(int i=0;i<targetAuthorities.size();i++){
			if(localAuthorities.contains(targetAuthorities.get(i))){
				deleteAuthorities.add(targetAuthorities.get(i));
			}
		}
		targetAuthorities.removeAll(deleteAuthorities);
		
		for (Authority targetAuthority : targetAuthorities) {
			Object[] params = new Object[]{
					targetAuthority.getId(),
					targetAuthority.getCreateDate(),
					targetAuthority.getUpdateDate(),
					targetAuthority.getVersion(),
					targetAuthority.getCode(),
					targetAuthority.getName(), 
					targetAuthority.getModuleId(),
					targetAuthority.getModuleName(),
					targetAuthority.getRemark()};
			jdbcTemplate.update(AuthorityDao.INSERT_AUTHORITY, params);
		}
		
		targetDbJdbcTemplate = null;
		
		targetAuthorities.clear();
		localAuthorities.clear();
		deleteAuthorities.clear();
	}
	
	/**
	 * 导入资源数据
	 * @param dc
	 */
	public void importResource(DatasourceConfig dc){
		JdbcTemplate targetDbJdbcTemplate = this.createJdbcTemplate(dc);
		
		//目标数据库中查询出的资源列表
		List<Resource> targetResources = targetDbJdbcTemplate.query("SELECT * FROM RP_SYSTEM_RESOURCES", 
				ParameterizedBeanPropertyRowMapper.newInstance(Resource.class));
		
		List<Resource> localResources = resourceDao.getAll();
		
		//临时保存待删除的本地已经拥有的资源列表
		List<Resource> deleteResources = new ArrayList<Resource>();
		for(int i=0;i<targetResources.size();i++){
			if(localResources.contains(targetResources.get(i))){
				deleteResources.add(targetResources.get(i));
			}
		}
		targetResources.removeAll(deleteResources);
		
		for (Resource targetResource : targetResources) {
			Object[] params = new Object[]{
					targetResource.getId(),
					targetResource.getCreateDate(),
					targetResource.getUpdateDate(),
					targetResource.getVersion(),
					targetResource.getName(),
					targetResource.getRemark(),
					targetResource.getType(),
					targetResource.getUrl(),
					targetResource.getModuleId(),
					targetResource.getModuleName()};
			jdbcTemplate.update(ResourceDao.INSERT_RESOURCE, params);
		}
		
		targetDbJdbcTemplate = null;
		targetResources.clear();
		localResources.clear();
		deleteResources.clear();
	}
	
	/**
	 * 导入角色数据
	 * @param dc
	 */
	public void importRole(DatasourceConfig dc){
		JdbcTemplate targetDbJdbcTemplate = this.createJdbcTemplate(dc);
		
		//目标数据库中查询出的角色列表
		List<Role> targetRoles = targetDbJdbcTemplate.query("SELECT * FROM RP_SYSTEM_ROLES", 
				ParameterizedBeanPropertyRowMapper.newInstance(Role.class));
		
		List<Role> localRoles = roleDao.getAll();
		
		//临时保存待删除的本地已经拥有的角色列表
		List<Role> deleteRoles = new ArrayList<Role>();
		for(int i=0;i<targetRoles.size();i++){
			if(localRoles.contains(targetRoles.get(i))){
				deleteRoles.add(targetRoles.get(i));
			}
		}
		targetRoles.removeAll(deleteRoles);
		
		for (Role targetRole : targetRoles) {
			Object[] params = new Object[]{
					targetRole.getId(),
					targetRole.getCreateDate(),
					targetRole.getUpdateDate(),
					targetRole.getVersion(),
					targetRole.getName(),
					targetRole.getRemark(),
					targetRole.getSuperRole()};
			jdbcTemplate.update(RoleDao.INSERT_ROLE, params);
		}
		
		targetDbJdbcTemplate = null;
		targetRoles.clear();
		localRoles.clear();
		deleteRoles.clear();
		
	}
	
	/**
	 * 导入信息栏目
	 * @param dc
	 */
	public void importChannel(DatasourceConfig dc){
		JdbcTemplate targetDbJdbcTemplate = this.createJdbcTemplate(dc);
		
		//目标数据库中查询出的栏目列表
		List<Channel> targetChannels = targetDbJdbcTemplate.query("SELECT * FROM RP_INFOPUB_CHANNELS", 
				ParameterizedBeanPropertyRowMapper.newInstance(Channel.class));
		
		List<Channel> localChannels = channelDao.getAll();
		
		//临时保存待删除的本地已经拥有的栏目列表
		List<Channel> deleteChannels = new ArrayList<Channel>();
		for(int i=0;i<targetChannels.size();i++){
			if(localChannels.contains(targetChannels.get(i))){
				deleteChannels.add(targetChannels.get(i));
			}
		}
		targetChannels.removeAll(deleteChannels);
		
		for (Channel targetChannel : targetChannels) {
			Object[] params = new Object[]{
					targetChannel.getId(),
					targetChannel.getCreateDate(),
					targetChannel.getUpdateDate(),
					targetChannel.getVersion(),
					targetChannel.getEnabled2(),
					targetChannel.getName(),
					targetChannel.getPid(),
					targetChannel.getCreatorId(),
					targetChannel.getCreatorName(),
					targetChannel.getIp(),
					targetChannel.getRemark()};
			jdbcTemplate.update(ChannelDao.INSERT_CHANNEL, params);
		}
		
		targetDbJdbcTemplate = null;
		targetChannels.clear();
		localChannels.clear();
		deleteChannels.clear();
	}
	
	/**
	 * 获取来源数据库中的栏目列表
	 * @param dc
	 * @return
	 */
	public List<Channel> getTargetDbChannels(DatasourceConfig dc){
		JdbcTemplate targetDbJdbcTemplate = this.createJdbcTemplate(dc);
		List<Channel> targetChannels = targetDbJdbcTemplate.query("SELECT * FROM RP_INFOPUB_CHANNELS", 
				ParameterizedBeanPropertyRowMapper.newInstance(Channel.class));
		targetDbJdbcTemplate = null;
		return targetChannels;
	}
	
	/**
	 * 导入文章数据
	 * @param dc
	 * @param targetChannelId 在来源数据库中需要导入的文章所述的信息栏目ID
	 */
	public void importArticle(DatasourceConfig dc, String targetChannelId){
		JdbcTemplate targetDbJdbcTemplate = this.createJdbcTemplate(dc);
		
		//目标数据库中查询出的文章列表
		List<Article> targetArticles = 
				targetDbJdbcTemplate.query("SELECT * FROM RP_INFOPUB_ARTICLES WHERE channelId = '" + targetChannelId +"'", 
				ParameterizedBeanPropertyRowMapper.newInstance(Article.class));
		
		List<Article> localArticles = articleDao.find("from Article where channelId = ?", targetChannelId);
		
		//临时保存待删除的本地已经拥有的文章列表
		List<Article> deleteArticles = new ArrayList<Article>();
		for(int i=0;i<targetArticles.size();i++){
			if(localArticles.contains(targetArticles.get(i))){
				deleteArticles.add(targetArticles.get(i));
			}
		}
		targetArticles.removeAll(deleteArticles);
		
		for (Article targetArticle : targetArticles) {
			Object[] params = new Object[]{
					targetArticle.getId(),
					targetArticle.getCreateDate(),
					targetArticle.getUpdateDate(),
					targetArticle.getVersion(),
					targetArticle.getChannelId(),
					targetArticle.getContent(),
					targetArticle.getSummary(),
					targetArticle.getTitle(),
					targetArticle.getCreatorId(),
					targetArticle.getCreatorName(),
					targetArticle.getIp(),
					targetArticle.getChannelName(),
					targetArticle.getClickNum(),
					targetArticle.getEnabled2()};
			jdbcTemplate.update(ArticleDao.INSERT_ARTICLE, params);
		}
		
		targetDbJdbcTemplate = null;
		targetArticles.clear();
		localArticles.clear();
		deleteArticles.clear();
		
	}
	
	
	
	
	private JdbcTemplate createJdbcTemplate(DatasourceConfig dc){
		DriverManagerDataSource ds = RpUtil.getDriverManagerDataSource(
				dc.getDbType(), dc.getUrl(), dc.getUsername(), dc.getPassword());
		
		JdbcTemplate targetDbJdbcTemplate = new JdbcTemplate();
		targetDbJdbcTemplate.setDataSource(ds);
		return targetDbJdbcTemplate;
	}
	
}
